package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.AuditStatusEnum;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证审核表 服务实现类
 * </p>
 *
 * @author jianghaijun
 * @since 2025-10-23
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {

    @Resource
    private WorkerCertificationMapper workerCertificationMapper;

    /**
     * 提交认证
     * @param workerCertificationAuditAddReqDTO 认证参数
     */
    @Override
    @Transactional
    public void submit(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {
        // 保存到认证表
        WorkerCertification workerCertification = BeanUtils.copyBean(workerCertificationAuditAddReqDTO, WorkerCertification.class);
        workerCertification.setId(workerCertificationAuditAddReqDTO.getServeProviderId());
        workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        workerCertification.setCreateTime(LocalDateTime.now());
        workerCertification.setUpdateTime(LocalDateTime.now());
        workerCertificationMapper.insert(workerCertification);
        // 保存到审核表
        // 拷贝参数
        WorkerCertificationAudit workerCertificationAudit = BeanUtils.copyBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);
        workerCertificationAudit.setAuditStatus(AuditStatusEnum.NOT_AUDIT.getStatus()); // 设置审核状态
        workerCertificationAudit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus()); // 设置认证状态
        workerCertificationAudit.setCreateTime(LocalDateTime.now());
        workerCertificationAudit.setUpdateTime(LocalDateTime.now());
        // todo 申请不通过，再次申请
        // 保存到审核表
        baseMapper.insert(workerCertificationAudit);
    }

    /**
     * 查询最新的驳回原因
     * @return 驳回原因
     */
    @Override
    public String rejectReason() {
        // 获取用户id
        Long id = UserContext.getCurrentUser().getId();
        // 查询条件
        LambdaQueryWrapper<WorkerCertificationAudit> wrapper = Wrappers.lambdaQuery(WorkerCertificationAudit.class)
                .select(WorkerCertificationAudit::getRejectReason)
                .eq(WorkerCertificationAudit::getServeProviderId, id)
                .orderByDesc(WorkerCertificationAudit::getCreateTime)
                .last("LIMIT 1");
        WorkerCertificationAudit audit = baseMapper.selectOne(wrapper);
        return ObjectUtils.isNotEmpty(audit) ? audit.getRejectReason() : "";
    }

    /**
     * 分页查询服务者认证申请
     *
     * @param workerCertificationAuditPageQueryReqDTO 分页查询参数
     * @return 分页结果
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO> page(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        Page<WorkerCertificationAudit> workerCertificationAuditPage = baseMapper.selectPage(PageUtils.parsePageQuery(workerCertificationAuditPageQueryReqDTO, WorkerCertificationAudit.class), Wrappers.lambdaQuery());
        return PageUtils.toPage(workerCertificationAuditPage, WorkerCertificationAuditResDTO.class);
    }

    /**
     * 审核认证信息
     * @param certificationAuditReqDTO 审核结果参数
     */
    @Override
    public void audit(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        // 更新审核表
        WorkerCertificationAudit workerCertificationAudit = baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(workerCertificationAudit)) {
            throw new BadRequestException("不存在该审核信息！");
        }
        workerCertificationAudit.setAuditStatus(AuditStatusEnum.AUDITED.getStatus()); // 设置审核状态为已审核
        workerCertificationAudit.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus()); // 根据传入的认证状态设置
        // 当驳回时，需要传驳回原因
        if (certificationAuditReqDTO.getCertificationStatus() == CertificationStatusEnum.FAIL.getStatus() && StringUtils.isEmpty(certificationAuditReqDTO.getRejectReason())) {
            throw new BadRequestException("请选择/输入驳回原因！");
        }
        workerCertificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason()); // 设置驳回原因
        workerCertificationAudit.setAuditorId(UserContext.getCurrentUser().getId()); // 设置审核人id
        workerCertificationAudit.setAuditorName(UserContext.currentUser().getName()); // 设置审核人姓名
        workerCertificationAudit.setAuditTime(LocalDateTime.now()); // 设置审核时间
        workerCertificationAudit.setUpdateTime(LocalDateTime.now()); // 设置更新时间

        baseMapper.updateById(workerCertificationAudit);

        // 更新认证表
        WorkerCertification workerCertification = workerCertificationMapper.selectById(workerCertificationAudit.getServeProviderId());
        if (ObjectUtils.isEmpty(workerCertification)) {
            throw new BadRequestException("不存在该认证信息！");
        }
        workerCertification.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus()); // 设置认证状态
        workerCertification.setCertificationTime(LocalDateTime.now()); // 设置认证时间
        workerCertification.setUpdateTime(LocalDateTime.now()); // 设置更新时间
        workerCertificationMapper.updateById(workerCertification);
    }
}
